
from Tkinter import *
import ctypes

SET_SERVO = 1
##CLR_RA1 = 2
##GET_RA2 = 3
##SET_DUTY = 4
global servospot
servospot=0

print "set servospot"


usb = ctypes.cdll.LoadLibrary('usb.dll')
usb.initialize()
print "hey o"
buffer = ctypes.c_buffer(8)

##def set_ra1_callback():
##    usb.control_transfer(dev, 0x40, SET_RA1, 0, 0, 0, buffer)
##
##def clr_ra1_callback():
##    usb.control_transfer(dev, 0x40, CLR_RA1, 0, 0, 0, buffer)

# def get_ra2_callback():
#     usb.control_transfer(dev, 0xC0, GET_RA2, 0, 0, 1, buffer)
#     status.configure(text = 'RA2 is currently %d.' % ord(buffer[0]))

##def set_duty_callback(value):
##    usb.control_transfer(dev, 0x40, SET_DUTY, int(value), 0, 0, buffer)
def set_servo_callback(value):
    global servospot
    servospot=int(value)
    usb.control_transfer(dev, 0x40, SET_SERVO, int(value), 0, 0, buffer)
##    
##def set_callback(pin):
##    global player
##    if not (pin in opponentpin or pin in playerpin):
##        usb.control_transfer(dev, 0x40, pin, 0, 0, 0, buffer)
##        img=PhotoImage(file=['X','O'][int(player)]+'.gif')
##        buttons[pin].configure(image=img)
##        buttons[pin].image=img
##        if player:
##            opponentpin.append(pin)
##        else:
##            playerpin.append(pin)
##        player = not player
    
##def make_callback(pin):
##    return lambda: set_callback(pin)

def update_status():
    global servospot
    #usb.control_transfer(dev, 0xC0, servospot, 0, 0, 1, buffer)
##    usb.control_transfer(dev, 0xC0, SET_SERVO, servospot, 0, 1, buffer)
    status.configure(text = 'Servo at %d' % servospot)
##    servospot+=1
##    if servospot>255:
##        servospot=0
    root.after(50, update_status)
        
root = Tk()
root.title('Tower GUI')
##buttons=[]
##img=PhotoImage(file='blank.GIF')
##for i in xrange(9):
##    
####    print img
##    b=Button(root,  width=64,height=64, text=' ', image=img,command = make_callback(i))
##    b.image=img
##    b.grid(row=i/3, column=i%3)
##    buttons.append(b)
    #b.pack(side = LEFT, padx=2, pady=2)
    

#Button(fm, text = ' ', command = clr_ra1_callback).pack(side = LEFT)
# Button(fm, text = 'GET_RA2', command = get_ra2_callback).pack(side = LEFT)
#fm.pack(side = TOP)
#dutyslider = Scale(root, from_ = 0, to = 255, orient = HORIZONTAL, showvalue = FALSE, command = set_duty_callback)
#dutyslider.set(128)
#dutyslider.pack(side = TOP)
dutyslider = Scale(root, from_ = 0, to = 255, orient = HORIZONTAL, showvalue = FALSE, command = set_servo_callback)
dutyslider.set(128)
dutyslider.pack(side = TOP)
status = Label(root, text = 'Get Ready for FUN.')
#status.grid(row=3,column=0,columnspan=3)
status.pack(side = TOP)
#Button(fm, text='kill/unkill',command = toggle_kill).pack(side = LEFT)

dev = usb.open_device(0x6666, 0x0003, 0)
if dev<0:
    print "No matching device found...\n"
else:
    ret = usb.control_transfer(dev, 0x00, 0x09, 1, 0, 0, buffer)
    if ret<0:
        print "Unable to send SET_CONFIGURATION standard request.\n"
    root.after(30, update_status)
    root.mainloop()
    usb.close_device(dev)



##global killed=False
##def toggle_kill():
##    global killed
##    value=0;
##    if 
##    servospot=int(value)
##    usb.control_transfer(dev, 0x40, SET_SERVO, int(value), 0, 0, buffer)
##    
